﻿using System;
using System.Collections.Generic;
using System.Linq;
using ChiWalker.Utilities;
using Styx;
using Styx.Common.Helpers;
using Styx.Patchables;
using Styx.WoWInternals.WoWObjects;

namespace ChiWalker.Helpers
{
    public static class BossList
    {
        private static readonly HashSet<uint> Bosses = new HashSet<uint>();

        public static HashSet<uint> IgnoreMobs = new HashSet<uint>
        {
            52288,
            // Venomous Effusion (NPC near the snake boss in ZG. Its the green lines on the ground. We want to ignore them.)
            52302,
            // Venomous Effusion Stalker (Same as above. A dummy unit)
            52320, // Pool of Acid
            52525, // Bloodvenom

            52387, // Cave in stalker - Kilnara
        };


        private static readonly HashSet<uint> Dummies = new HashSet<uint>
        {
            31146, // Raider's
            46647, // 81-85
            32546, // Ebon Knight's (DK)
            31144, // 79-80
            32543, // Veteran's (Eastern Plaguelands)
            32667, // 70
            32542, // 65 EPL
            32666, // 60
            30527, // ?? Boss one (no idea?)
        };

        private static readonly WaitTimer PopulateCd = new WaitTimer(TimeSpan.FromSeconds(10));
        private static uint _lastCheckMapId;

        static BossList()
        {
            PopulateBosses();
            foreach (uint bossId in Dummies)
            {
                Bosses.Add(bossId);
            }
        }

        public static HashSet<string> CurrentMapBosses { get; private set; }

        private static HashSet<uint> BossIds
        {
            get
            {
                foreach (uint bossId in Dummies)
                {
                    Bosses.Add(bossId);
                }

                return Bosses;
            }
        }

        public static HashSet<uint> TrainingDummies
        {
            get { return Dummies; }
        }

        /// <summary>
        ///     Checks id the unit is featured in the dungon journal, making it a boss
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static bool IsJournalBoss(this WoWUnit unit)
        {
            PopulateBosses();
            return (CurrentMapBosses != null && CurrentMapBosses.Contains(unit.Name)) ||
                   (BossIds != null && BossIds.Contains(unit.Entry));
        }

        public static void PopulateBosses()
        {
            if (!PopulateCd.IsFinished)
                return;

            if (_lastCheckMapId == StyxWoW.Me.MapId)
            {
                PopulateCd.Reset();
                return;
            }

            CurrentMapBosses = new HashSet<string>();
            CurrentMapBosses =
                new HashSet<string>(
                    StyxWoW.Db[ClientDb.DungeonEncounter].Where(r => r.GetField<int>(1) == StyxWoW.Me.MapId).Select(
                        r => r.GetStringField(5)));

            _lastCheckMapId = StyxWoW.Me.MapId;

            Logger.WriteVerbose("Boss List Updated for {0}", StyxWoW.Me.MapName);
            Logger.WriteVerbose("Found {0} bosses", CurrentMapBosses.Count);

            PopulateCd.Reset();
        }
    }
}